<template>
  <view class="collection-container">
    <view class="collection-list" v-if="collectionList.length > 0">
      <view class="collection-item" v-for="(item, index) in collectionList" :key="index" @click="goDetail(item)">
        <view class="item-header">
          <view class="title">{{ item.archives_title }}</view>
          <view class="time">{{ item.createtime }}</view>
        </view>
        <view class="item-content">
          <image v-if="item.image" class="cover-image" :src="item.image" mode="aspectFill"></image>
          <view class="summary">{{ item.description || '暂无描述' }}</view>
        </view>
        <view class="item-footer">
          <view class="stats">
            <view class="stat-item">
              <u-icon name="eye" size="28" color="#999"></u-icon>
              <text>{{ item.views || 0 }}</text>
            </view>
            <view class="stat-item">
              <u-icon name="chat" size="28" color="#999"></u-icon>
              <text>{{ item.comments || 0 }}</text>
            </view>
            <view class="stat-item">
              <u-icon name="thumb-up" size="28" color="#999"></u-icon>
              <text>{{ item.likes || 0 }}</text>
            </view>
          </view>
          <view class="actions">
            <u-button size="mini" type="error" @click.stop="cancelCollection(item)">取消收藏</u-button>
          </view>
        </view>
      </view>
    </view>
    
    <u-empty v-else text="暂无收藏" mode="list"></u-empty>
    
    <u-loadmore :status="loadStatus" />
    
    <!-- 取消收藏确认弹窗 -->
    <u-modal
      v-model="showCancelModal"
      title="取消确认"
      content="确定要取消收藏这篇文章吗？"
      :show-cancel-button="true"
      @confirm="confirmCancel"
    ></u-modal>
  </view>
</template>

<script>
import { collection } from '@/api/cms'

export default {
  data() {
    return {
      collectionList: [],
      page: 1,
      limit: 10,
      loadStatus: 'loadmore',
      showCancelModal: false,
      currentCancelItem: null
    }
  },
  onLoad() {
    this.getCollectionList()
  },
  onPullDownRefresh() {
    this.page = 1
    this.collectionList = []
    this.getCollectionList().then(() => {
      uni.stopPullDownRefresh()
    })
  },
  onReachBottom() {
    if (this.loadStatus === 'loadmore') {
      this.page++
      this.getCollectionList()
    }
  },
  methods: {
    // 获取收藏列表
    async getCollectionList() {
      try {
        this.loadStatus = 'loading'
        
        const params = {
          page: this.page,
          limit: this.limit
        }
        
        const { data } = await collection.getCollectionList(params)
        
        if (data.code === 1) {
          if (this.page === 1) {
            this.collectionList = data.data.rows
          } else {
            this.collectionList = [...this.collectionList, ...data.data.rows]
          }
          
          this.loadStatus = data.data.rows.length < this.limit ? 'nomore' : 'loadmore'
        } else {
          this.loadStatus = 'loadmore'
          uni.showToast({
            title: data.msg || '获取收藏列表失败',
            icon: 'none'
          })
        }
      } catch (error) {
        console.error('获取收藏列表失败', error)
        this.loadStatus = 'loadmore'
        uni.showToast({
          title: '获取收藏列表失败，请稍后再试',
          icon: 'none'
        })
      }
    },
    
    // 跳转到文章详情
    goDetail(item) {
      uni.navigateTo({
        url: `/pages_article/detail/index?id=${item.archives_id}`
      })
    },
    
    // 取消收藏
    cancelCollection(item) {
      this.currentCancelItem = item
      this.showCancelModal = true
    },
    
    // 确认取消收藏
    async confirmCancel() {
      if (!this.currentCancelItem) return
      
      try {
        const { data } = await collection.cancelCollection(this.currentCancelItem.archives_id)
        
        if (data.code === 1) {
          uni.showToast({
            title: '取消收藏成功',
            icon: 'success'
          })
          
          // 从列表中移除已取消收藏的文章
          const index = this.collectionList.findIndex(item => item.id === this.currentCancelItem.id)
          if (index !== -1) {
            this.collectionList.splice(index, 1)
          }
        } else {
          uni.showToast({
            title: data.msg || '取消收藏失败',
            icon: 'none'
          })
        }
      } catch (error) {
        console.error('取消收藏失败', error)
        uni.showToast({
          title: '取消收藏失败，请稍后再试',
          icon: 'none'
        })
      } finally {
        this.currentCancelItem = null
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.collection-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 20rpx;
}

.collection-item {
  background-color: #ffffff;
  border-radius: 12rpx;
  padding: 20rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16rpx;
  
  .title {
    font-size: 32rpx;
    font-weight: bold;
    color: #333333;
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  
  .time {
    font-size: 24rpx;
    color: #999999;
    margin-left: 20rpx;
  }
}

.item-content {
  display: flex;
  margin-bottom: 16rpx;
  
  .cover-image {
    width: 200rpx;
    height: 140rpx;
    border-radius: 8rpx;
    margin-right: 16rpx;
  }
  
  .summary {
    flex: 1;
    font-size: 28rpx;
    color: #666666;
    line-height: 1.5;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
  }
}

.item-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  
  .stats {
    display: flex;
    
    .stat-item {
      display: flex;
      align-items: center;
      margin-right: 20rpx;
      
      text {
        font-size: 24rpx;
        color: #999999;
        margin-left: 6rpx;
      }
    }
  }
  
  .actions {
    display: flex;
  }
}
</style>